package com.donger.business.upms.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.donger.business.upms.entity.SysConfigUser;
import com.donger.business.upms.service.ISysConfigUserService;
import com.donger.common.core.utils.Res;
import com.donger.common.core.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;


/**
 * <p>
 * 系统用户配置信息表 前端控制器
 * </p>
 *
 * @author 7326
 */
@AllArgsConstructor
@RestController
@RequestMapping("/sys/configUser")
@Api(tags = {"用户配置config"})
public class SysConfigUserController {

    private static final String MODULE_NAME = "系统用户配置管理";

    private final ISysConfigUserService sysConfigUserService;

    /**
     * 所有配置列表
     */
    @GetMapping("/page")
    @ApiOperation(value = "配置列表")
    public Result list(Page<SysConfigUser> page, SysConfigUser entity) {
        return Res.ok(sysConfigUserService.pageList(page, new QueryWrapper<SysConfigUser>()
                .like(StrUtil.isNotBlank(entity.getParamKey()), "a.param_key", entity.getParamKey())
        ));
    }

    /**
     * 配置信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "配置详情")
    public Result<SysConfigUser> info(@PathVariable("id") Long id) {
        return Res.ok(sysConfigUserService.getById(id));
    }

    /**
     * 保存配置
     */
    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('sys:configUser:save')")
    public Result<Boolean> save(@Valid @RequestBody SysConfigUser config) {
        return Res.ok(sysConfigUserService.save(config));
    }

    /**
     * 修改配置
     */
    @PostMapping("/update")
    @PreAuthorize("@pms.hasPermission('sys:configUser:update')")
    public Result<Boolean> update(@Valid @RequestBody SysConfigUser config) {
        return Res.ok(sysConfigUserService.updateById(config));
    }

    /**
     * 删除配置
     */
    @DeleteMapping("/del/{id}")
    @PreAuthorize("@pms.hasPermission('sys:configUser:del')")
    public Result<Boolean> delete(@PathVariable Serializable id) {
        return Res.ok(sysConfigUserService.removeById(id));
    }


}

